<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-calendar"></i>后台用户
        </el-breadcrumb-item>
        <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="form-box">
        <el-form ref="form" :model="form" label-width="200px">
          <el-form-item label="用户头像">
            <div class="crop-demo">
              <img :src="form.agentLogo" class="pre-img" />
              <input class="crop-input" type="file" name="image" accept="image/*" @change="setImage" />
            </div>
          </el-form-item>
          <el-form-item label="登陆用户名">
            <el-input v-model="form.userName" :disabled="isdisabled"></el-input>
          </el-form-item>
          <el-form-item label="android下载地址">
            <el-input v-model="list.androidDownloadLink"></el-input>
          </el-form-item>
          <el-form-item label="android版本">
            <el-input v-model="list.androidVersion"></el-input>
          </el-form-item>
          <el-form-item label="ios下载地址">
            <el-input v-model="list.iosDownloadLink"></el-input>
          </el-form-item>
          <el-form-item label="ios版本">
            <el-input v-model="list.iosVersion"></el-input>
          </el-form-item>
          <el-form-item label="邮件地址">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
          <el-form-item label="代理商ID">
            <el-input v-model="form.parentId" :readonly="true"></el-input>
          </el-form-item>
          <el-form-item label="RSA公钥">
            <el-input v-model="form.rsaPublicKey" :readonly="true" type="textarea" resize="none"
              :autosize="true"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">表单提交</el-button>
            <el-button>取消</el-button>
            <el-button v-if="form.userName === 'admin'" type="primary" @click="open">开户</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { upload, editUser, getUser, editUser2 } from "../../api/index";
export default {
  name: "baseform",
  data() {
    return {
      form: this.$store.state.adminMsg,
      isdisabled: false,
      list: {}
    };
  },
  mounted() {
    if (this.form.userName === 'admin') {
      this.isdisabled = true
    }
  },
  methods: {
    setImage(file) {
      let imgFile = file.target.files[0];
      let formData = new FormData();
      formData.append("file", imgFile);
      upload(formData).then((res) => {
        this.form.agentLogo = res.data[0].url;
      });
    },
    getUser() {
      getUser({
        parentId: localStorage.getItem("parentId")
      }).then((res) => {
        if (res.code === 2000) {
          console.log(res);
          this.list = res.data
        } else {
          this.$message.error(res.message);
        }
      });
    },
    onSubmit() {
      editUser({
        androidDownloadLink: this.list.androidDownloadLink,
        androidVersion: this.list.androidVersion,
        id: this.list.id,
        iosDownloadLink: this.list.iosDownloadLink,
        iosVersion: this.list.iosVersion
      }).then((res) => {
        if (res.code === 2000) {
          this.$message.success(res.message);
          this.reload();
        } else {
          this.$message.error(res.message);
        }
      });
      editUser2({
        agentLogo: this.form.agentLogo,
        email: this.form.email,
        id: this.form.id,
        mobile: this.form.mobile,
        userName: this.form.userName,
      }).then((res) => {
        if (res.code === 2000) {
          this.$store.commit("getAdminMsg", this.form);
          this.$message.success(res.message);
          this.reload();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    open() {
      editUser({
        userName: this.form.userName,
      }).then((res) => {
        if (res.code === 2000) {
          this.$message.success(res.message);
          this.reload();
        } else {
          this.$message.error(res.message);
        }
      });
    }
  },
  created() {
    this.getUser();
  },
};
</script>
<style scoped>
.pre-img {
  width: 100px;
  height: 100px;
  background: #f8f8f8;
  border: 1px solid #eee;
  border-radius: 5px;
}

.crop-demo {
  display: flex;
  align-items: flex-end;
}

.crop-input {
  position: absolute;
  width: 102px;
  height: 102px;
  left: 0px;
  top: 0px;
  opacity: 0;
  cursor: pointer;
}
</style>